import $api from "@/api/index.js"
import store from "@/store/index.js"
import $store from "@/store/index.js"
export default {
	state: {
		pageActive: false,
		map: {
			latitude: 0,
			longitude: 0,
			scale: 16,
			controls: []
		},
		covers: [],
		service_cate: [],
		param: {
			activeIndex: 0,
			cate_id: 0,
		},
		mapList: []
	},
	mutations: {
		async updateMapItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		async getMapIndex({
			commit,
			state
		}, param) {
			let d = await $api.service.serviceCateList(param)
			d.unshift({
				id: 0,
				title: '全部'
			})
			commit('updateMapItem', {
				key: 'service_cate',
				val: d
			})
		},
		async getMapList({
			commit,
			state
		}, param) {
			let {
				city_id = 0
			} = $store.state.technician.param
			let {
				id: city_info_id = 0,
			} = $store.state.user.city_info
			let {
				title = ''
			} = $store.state.user.location
			city_id = city_id || city_info_id
			if (city_id) {
				param.city_id = city_id
			}
			delete param.activeIndex
			let d = await $api.service.mapCoachList(param)
			let map = Object.assign({}, state.map, {
				latitude: param.lat,
				longitude: param.lng,
			})
			let arr = []
			d.map((item, index) => {
				arr.push({
					id: index,
					latitude: item.lat,
					longitude: item.lng,
					iconPath: item.work_img,
					width: 40,
					height: 40
				})
			})
			commit('updateMapItem', {
				key: 'map',
				val: map
			})
			commit('updateMapItem', {
				key: 'covers',
				val: arr
			})
			commit('updateMapItem', {
				key: 'mapList',
				val: d
			})
		}
	},
}